package com.kyxx.topics.net;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;

import com.kyxx.topics.R;
import com.kyxx.topics.widget.LoadingDialog;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.request.BaseRequest;

/**
 * Created by Administrator on 2016/12/2.
 */
public abstract class LoadingStringCallBack extends StringCallback {
    /**
     * 如果需要用到Dialog.context请传Activity
     */
    private Context context;

    private boolean hasDialog = true;

    private LoadingDialog dialog;
    private LinearLayout error;
    private LinearLayout loading;

    /**
     * 默认只有dialog加载样式
     *
     * @param context
     * @param
     */
    public LoadingStringCallBack(Context context) {
        this(context, true);
    }

    /**
     * 无dialog加载
     *
     * @param context
     * @param noDialog
     */
    public LoadingStringCallBack(Context context, boolean noDialog) {
        this.context = context;
        this.hasDialog = noDialog;
    }

    /**
     * 无dialog加载,有界面加载样式
     *
     * @param context
     * @param
     */
    public LoadingStringCallBack(Context context, LinearLayout loading,
                                 LinearLayout error) {
        this.hasDialog = false;
        this.context = context;
        this.loading = loading;
        this.error = error;
    }

    @Override
    public void onBefore(BaseRequest request) {
        super.onBefore(request);
        if (hasDialog) {
            dialog = new LoadingDialog(context, R.style.Loading_Dialog);
            dialog.show();
        }
        if (loading != null) {
            loading.setVisibility(View.VISIBLE);
        }
        if (error != null) {
            error.setVisibility(View.GONE);
        }
    }

    @Override
    public void onAfter(String s, Exception e) {
        super.onAfter(s, e);
        if (dialog != null) {
            dialog.dismiss();
        }
        if (loading != null) {
            loading.setVisibility(View.GONE);
        }
    }
}
